package com.exam.web.action.subject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.exam.bean.PageView;
import com.exam.bean.QueryResult;
import com.exam.bean.subject.KnowledgePoint;
import com.exam.service.subject.KnowledgePointService;
import com.exam.web.action.BaseAction;
import com.exam.web.action.privilege.Privilege;
import com.opensymphony.xwork2.ActionContext;

@Controller("knowledgePointAction")
@Scope("prototype")
public class KnowledgePointAction extends BaseAction {
	
	private static final long serialVersionUID = 6602054230660392392L;

	@Resource
	private KnowledgePointService knowledgePointService;
	
	private PageView<KnowledgePoint> pageView;//记录集
	private KnowledgePoint point;//组织结构
	private Integer id;//id
	private Integer parentid;//父级id
	private Integer[] pointids;
	
	/**
	 * 知识点显示
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="point",privilegeValue="view")
	public String list()throws Exception{
		
		PageView<KnowledgePoint> pageView=new PageView<KnowledgePoint>(12,this.getPage());
		int firstIndex=(this.getPage()-1)*pageView.getMaxResult();
		
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		
		StringBuffer jpql=new StringBuffer("");
		List<Object> params=new ArrayList<Object>();
		if("true".equals(this.getQuery())){
			if(this.getPoint().getName()!=null && !"".equals(this.getPoint().getName().trim())){
				jpql.append("o.name like ?"+ (params.size()+1));
				params.add("%"+ this.getPoint().getName().trim()+ "%");
			}
			else{
				QueryResult<KnowledgePoint> qr=knowledgePointService.getScrollData(KnowledgePoint.class
						,firstIndex,pageView.getMaxResult(),orderby);
				pageView.setQueryResult(qr);
				this.setPageView(pageView);
				return SUCCESS;
			}
		}
		else{
			if(this.getParentid()!=null&&this.getParentid()>0){
				jpql.append("o.parent.id=?"+(params.size()+1));
				params.add(this.getParentid());
			}
			else{
				jpql.append("o.parent is null");
			}
		}
		
		QueryResult<KnowledgePoint> qr=knowledgePointService.getScrollData(KnowledgePoint.class
				,firstIndex,pageView.getMaxResult(),jpql.toString(),params.toArray(),orderby);
		
		pageView.setQueryResult(qr);
		this.setPageView(pageView);
		
		return SUCCESS;
	}

	/**
	 * 添加知识点
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="point",privilegeValue="insert")
	public String add()throws Exception{
		
		KnowledgePoint point=this.getPoint();
		if(this.getParentid()!=null&&this.getParentid()>0){
			point.setParent(new KnowledgePoint(this.getParentid()));
		}
		knowledgePointService.save(point);
		ActionContext.getContext().put("message", "添加知识点成功");
		ActionContext.getContext().put("urladdress", "point_list.html");
		return "message";
	}
	
	/**
	 * 添加知识点界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="point",privilegeValue="insert")
	public String addUI()throws Exception{
		
		return "add";
	}
	
	/**
	 * 修改知识点
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="point",privilegeValue="update")
	public String edit()throws Exception{
		
		KnowledgePoint point=knowledgePointService.find(KnowledgePoint.class, this.getPoint().getId());
		point.setName(this.getPoint().getName());
		knowledgePointService.update(point);
		ActionContext.getContext().put("message", "修改知识点成功");
		ActionContext.getContext().put("urladdress", "point_list.html");
		return "message";
	}
	
	/**
	 * 修改知识点界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="point",privilegeValue="update")
	public String editUI()throws Exception{
		
		KnowledgePoint point=knowledgePointService.find(KnowledgePoint.class, this.getId());
		this.setPoint(point);
		return "edit";
	}
	
	/**
	 * 启用知识点
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="point",privilegeValue="visible")
	public String visible()throws Exception{
		
		knowledgePointService.setVisible(this.getPointids(), true);
		ActionContext.getContext().put("message", "启用成功");
		ActionContext.getContext().put("urladdress", "point_list.html");
		return "message";
	}
	
	/**
	 * 禁用知识点
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="point",privilegeValue="disvisible")
	public String disvisible()throws Exception{
		
		knowledgePointService.setVisible(this.getPointids(), false);
		ActionContext.getContext().put("message", "禁用成功");
		ActionContext.getContext().put("urladdress", "point_list.html");
		return "message";
	}

	public PageView<KnowledgePoint> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<KnowledgePoint> pageView) {
		this.pageView = pageView;
	}

	public KnowledgePoint getPoint() {
		return point;
	}

	public void setPoint(KnowledgePoint point) {
		this.point = point;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getParentid() {
		return parentid;
	}

	public void setParentid(Integer parentid) {
		this.parentid = parentid;
	}

	public Integer[] getPointids() {
		return pointids;
	}

	public void setPointids(Integer[] pointids) {
		this.pointids = pointids;
	}
	
	
}
